GitHub Branching and Collaboration
Purpose of Branches
Branches in GitHub allow for parallel development on different features or bugs without impacting the main or master branch. This approach enables multiple developers to contribute to a project simultaneously.
- Files in GitHub are stored on branches
- The master branch contains the deployable version of the code
- New branches are created for changes or new features
Creating a New Branch
To create a new branch:
- Access the
branch: master
drop-down menu - Enter the name for the new branch in the
new branch
field - Click
Create branch
Best Practices
- Use descriptive names for branches
Making Changes in a Branch
Changes to files within a branch are made by:
- Selecting the file
- Clicking the pencil icon
- Editing the file
- Committing the changes
Committing Changes
- Ensure code is stable before committing
- Commit messages should be meaningful and follow best practices (e.g., no period at the end, under 50 characters, use active voice)
Pull Requests
Pull requests propose changes from one branch to another (typically to master
) and require approval.
To create a pull request:
- Click
Pull request
>New pull request
- Choose the new branch to compare
- Review the changes
- Add a title and description
- Click
Create pull request
Important Information
- Pull requests can be made with unfinished code
- Approval can be by the author or assigned team members
Merging Changes
To integrate changes from a pull request:
- Click
Merge pull request
- Click
Confirm merge
- Delete the feature branch post-merge as it becomes obsolete
Objectives and Introduction
GitHub, hosting over 100 million repositories, offers tools for collaboration such as forking and cloning repositories, essential for both team and independent project development.
Cloning a Repository
Cloning creates a local copy of a repository.
Steps to Clone
- Navigate to the repository on GitHub
- Click
Code
> clipboard icon under "Clone with HTTPS" - In a terminal, navigate to the target directory and run
git clone <Pasted_URL>
Keeping Sync
- Use
git add
,git commit -m <message>
, andgit push
to sync changes with the remote repository
Forking a Repository
Forking creates a separate copy of a repository for independent changes.
Steps to Fork
- Go to the repository to fork
- Click
Fork
at the top-right
Using a Fork
- Forks allow for independent project development or contributions back to the original project
Legal and Best Practices
- It's often required to keep a copy of the license file when forking
Collaborating Using Git Commands
Remote Repositories are managed with commands like git push
(send changes), git fetch
(pull changes without merging), and git pull
(fetch and merge).
Terminology
Origin
: Your fork of a repositoryUpstream
: The original repository
Syncing a Fork
- Use
git remote -v
to view remote repositories - Add the original repository as
upstream
withgit remote add upstream <Original_Repo_URL>
- Update with
git fetch upstream
and merge usinggit merge upstream/master
orgit pull upstream
Roles in Git Projects
Developer
Utilizes commands like git-clone
, git-pull
, git-push
, git-format-patch
, and git-send-email
for collaboration.
Integrator
Manages incoming changes, using commands like git-am
, git-pull
, git-format-patch
, git-revert
, and git-push
.
Repository Administrator
Sets up and maintains repository access, using tools like git-daemon
, git-shell
, git-http-backend
, gitweb
, and GitHub Actions for automation.
Summary
- The roles of Developer, Integrator, and Repository Administrator are key to managing a project, each with specific Git commands and tools for effective communication and collaboration.